package com.hzhanghuan.app;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.hzhanghuan.recyclerview.ItemEventHelper;
import com.hzhanghuan.recyclerview.adapter.BaseAdapter;

/**
 * Created by Administrator on 2016/5/9.
 */
public class PageActivity extends AppCompatActivity {


    RecyclerView recyclerView;
    private BaseAdapter<String, VH> adapter;
    private ItemEventHelper itemEventHelper = new ItemEventHelper();
    Toast toast;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.page_activity);
        recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new BaseAdapter<String, VH>() {
            @Override
            public VH onCreateViewHolder(ViewGroup parent, int viewType) {
                return new VH(parent);
            }


            @Override
            public void onBindViewHolder(VH holder, int position) {
                String item = getItem(position);
                holder.textView.setText(item);
            }


        };
        recyclerView.setAdapter(adapter);
        adapter.add("1");
        adapter.add("2");
        adapter.add("3");
        adapter.add("4");
        itemEventHelper.setup(recyclerView);
        itemEventHelper.setItemRootViewClickListener(new ItemEventHelper.ItemRootViewClickListener() {
            @Override
            public void onItemClick(RecyclerView.ViewHolder viewHolder) {
                if (toast != null) {
                    toast.cancel();
                }
                toast = Toast.makeText(viewHolder.itemView.getContext(), String.valueOf(viewHolder.getAdapterPosition()), Toast.LENGTH_SHORT);
                toast.show();
            }
        });
        itemEventHelper.setItemRootViewLongClickListener(new ItemEventHelper.ItemRootViewLongClickListener() {
            @Override
            public boolean onItemLongClick(RecyclerView.ViewHolder viewHolder) {
                if (toast != null) {
                    toast.cancel();
                }
                toast = Toast.makeText(viewHolder.itemView.getContext(), "Long:" + String.valueOf(viewHolder.getAdapterPosition()), Toast.LENGTH_SHORT);
                toast.show();


                return true;
            }
        });


    }



    class VH extends RecyclerView.ViewHolder {


        TextView textView;


        public VH(ViewGroup parent) {
            super(LayoutInflater.from(parent.getContext()).inflate(android.R.layout.simple_list_item_1, parent, false));
            textView = (TextView) itemView.findViewById(android.R.id.text1);

        }
    }

}
